package com.a2j.web.car.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.a2j.beans.base.BaseBean;
import com.a2j.beans.base.BasePageBean;
import com.a2j.beans.base.code.ResponseCode;
import com.a2j.beans.car.sub_brand.bo.CarSubBrandBO;
import com.a2j.beans.car.sub_brand.bo.CarSubBrandPagesBO;
import com.a2j.beans.car.sub_brand.dto.CarSubBrandDTO;
import com.a2j.beans.car.sub_brand.dto.CarSubBrandRequestDTO;
import com.a2j.beans.car.sub_brand.po.CarSubBrandPO;
import com.a2j.beans.car.sub_brand.vo.CarSubBrandByBrandIdVO;
import com.a2j.beans.car.sub_brand.vo.CarSubBrandVO;
import com.a2j.beans.enums.SourceTypeEnum;
import com.a2j.exception.BizException;
import com.a2j.utils.helper.PageHelper;
import com.a2j.web.car.mapper.CarSubBrandMapper;
import com.a2j.web.car.service.CarSubBrandService;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: bzb
 * Data: 2021/11/18 21:33
 * Desc: 子品牌实现
 */
@Service
public class CarSubBrandImpl implements CarSubBrandService {

    @Resource
    CarSubBrandMapper mapper;

    @Override
    public void addCarSubBrand(CarSubBrandDTO dto) {
        Assert.notNull(dto, "子品牌对象为null!");
        Assert.notNull(dto.getBrandId(), "品牌id为null!");
        Assert.hasLength(dto.getSubBrandName(), "子品牌名称为null!");

        // 入参风险控制,忽略不需要的参数 DTO 转 VO
        val po = CarSubBrandPO.builder()
                .name(dto.getSubBrandName())
                .brandId(dto.getBrandId())
                .sourceType(dto.getSourceType())
                .remark(dto.getRemark())
                .build();

        int result = mapper.addCarSubBrand(po);
        if (result == 0) throw new BizException(ResponseCode.FAILED_ADD);
    }

    @Override
    public BaseBean<BasePageBean<CarSubBrandVO>> getAllCarSubBrands(CarSubBrandRequestDTO record) {
        Assert.notNull(record, "子品牌分页对象为null!");

        PageHelper.checkPage(record);

        val bo = CarSubBrandPagesBO.builder()
                .brandName(record.getBrandName())
                .subBrandName(record.getSubBrandName())
                .sourceType(record.getSourceType())
                .enable(record.getEnable())
                .createTime(record.getCreateTime())
                .page(record.getPage())
                .rows(record.getRows())
                .build();

        List<CarSubBrandBO> allCarSubBrands = mapper.getAllCarSubBrands(bo);
        if (allCarSubBrands == null || allCarSubBrands.isEmpty()) {
            throw new BizException(ResponseCode.NO_DATA);
        }

        // 处理货源渠道
        for (CarSubBrandBO subBrandBO : allCarSubBrands) {
            if (subBrandBO.getSourceType() == null) continue;
            subBrandBO.setSourceTypeStr(SourceTypeEnum.getDesc(subBrandBO.getSourceType()));
        }

        List<CarSubBrandVO> vos = BeanUtil.copyToList(allCarSubBrands, CarSubBrandVO.class);
        if (vos == null || vos.isEmpty()) {
            throw new BizException(ResponseCode.NO_DATA);
        }

        // 总数
        int total = mapper.selectCount(bo);

        return new PageHelper<CarSubBrandVO>().transformData(record.getPage(), record.getRows(), total, vos);
    }

    @Override
    public BaseBean<List<String>> getAllSourceTypes() {
        val allSourceTypes = mapper.getAllSourceTypes();
        if (allSourceTypes == null || allSourceTypes.isEmpty()) {
            throw new BizException(ResponseCode.NO_DATA);
        }

        // 数据库存的是int,转成中文描述返回给前端
        val list = new ArrayList<String>();
        for (Integer sourceType : allSourceTypes) {
            String desc = SourceTypeEnum.getDesc(sourceType);
            if (!"".equals(desc)) list.add(desc);
        }

        return BaseBean.success(list);
    }

    @Override
    public BaseBean<CarSubBrandVO> getCarSubBrandDetail(Integer id) {
        Assert.notNull(id, "品牌id为null!");

        val detail = mapper.getCarSubBrandDetail(id);
        if (detail == null) throw new BizException(ResponseCode.NO_DATA);

        // 出参 bo -> vo
        val detailVO = CarSubBrandVO.builder().build();
        BeanUtils.copyProperties(detail, detailVO);

        // 处理货源渠道
        detailVO.setSourceTypeStr(SourceTypeEnum.getDesc(detailVO.getSourceType()));

        return BaseBean.success(detailVO);
    }

    @Override
    public BaseBean<List<CarSubBrandByBrandIdVO>> carSubBrandByBrandId(Integer id) {
        Assert.notNull(id, "brandId为null!");

        List<CarSubBrandPO> subBrandPOList = mapper.carSubBrandByBrandId(id);
        if (subBrandPOList == null) throw new BizException(ResponseCode.NO_DATA);

        // 出参转 VO
        List<CarSubBrandByBrandIdVO> subBrandVOList = BeanUtil.copyToList(subBrandPOList, CarSubBrandByBrandIdVO.class);
        if (subBrandVOList == null) throw new BizException(ResponseCode.NO_DATA);
        for (CarSubBrandByBrandIdVO vo : subBrandVOList) {
            vo.setSourceTypeStr(SourceTypeEnum.getDesc(vo.getSourceType()));
        }

        return BaseBean.success(subBrandVOList);
    }

    @Override
    public void deleteCarSubBrand(Integer id) {
        Assert.notNull(id, "brandId为null!");

        int result = mapper.deleteCarSubBrand(id);
        if (result == 0) throw new BizException(ResponseCode.FAILED_DELETE);
    }


    @Override
    public void enable(Integer id, Integer enable) {
        Assert.notNull(id, "子品牌id为null!");
        if (enable == null) enable = 0;

        val carSubBrandPO = CarSubBrandPO.builder()
                .id(id)
                .enable(enable)
                .build();

        int result = mapper.updateCarSubBrand(carSubBrandPO);
        if (result == 0) throw new BizException(ResponseCode.FAILED_UPDATE);
    }

    @Override
    public void updateCarSubBrand(CarSubBrandDTO record) {
        Assert.notNull(record, "子品牌对象为null!");
        Assert.notNull(record.getBrandId(), "品牌id为null!");
        Assert.notNull(record.getSubBrandId(), "子品牌id为null!");
        Assert.hasLength(record.getSubBrandName(), "子品牌名称为null!");

        val carSubBrandPO = CarSubBrandPO.builder()
                .id(record.getSubBrandId())
                .brandId(record.getBrandId())
                .name(record.getSubBrandName())
                .sourceType(record.getSourceType())
                .enable(record.getEnable())
                .remark(record.getRemark())
                .build();

        int result = mapper.updateCarSubBrand(carSubBrandPO);
        if (result == 0) throw new BizException(ResponseCode.FAILED_UPDATE);
    }
}
